package com.tjise.vod.service.impl;

import com.aliyun.vod.upload.impl.UploadVideoImpl;
import com.aliyun.vod.upload.req.UploadStreamRequest;
import com.aliyun.vod.upload.resp.UploadStreamResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.vod.model.v20170321.DeleteVideoRequest;
import com.tjise.commonutils.R;
import com.tjise.servicebase.exceptionhandler.TjiseException;
import com.tjise.vod.service.VodService;
import com.tjise.vod.util.ConstantVodUtils;
import com.tjise.vod.util.InitVdoClient;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @program: tjise_shixun
 * @description: 视频管理的业务类
 * @author: 郑朝文
 * @create: 2021-06-25 18:12
 **/
@Service
@Slf4j
public class VodServiceImpl implements VodService {
    /**
     * 上传视频进入阿里云的视频点播
     *
     * @param file
     * @return
     */
    @Override
    public String uploadVideo(MultipartFile file) {
        InputStream fis = null;
        // 获取存入的id值
        String videoId = null;

        try {
            // 获取文件的名字
            String fileName = file.getOriginalFilename();
            // 上传的名字
            String title = fileName.substring(0, fileName.indexOf("."));
            // 自己的 accessKeyId 值
            String accessKeyId = ConstantVodUtils.ACCESS_KEY_ID;
            // 自己的  accessKeySecret 的值
            String accessKeySecret = ConstantVodUtils.ACCESS_KEY_SECRET;
            fis = file.getInputStream();
            UploadStreamRequest request = new UploadStreamRequest(accessKeyId, accessKeySecret, title, fileName, fis);
            UploadVideoImpl uploader = new UploadVideoImpl();
            UploadStreamResponse response = uploader.uploadStream(request);

            if (response.isSuccess()) {
                // 获取文件上传的id
                videoId = response.getVideoId();
            } else { //如果设置回调URL无效，不影响视频上传，可以返回VideoId同时会返回错误码。其他情况上传失败时，VideoId为空，此时需要根据返回错误码分析具体错误原因
                videoId = response.getVideoId();
            }


            log.info("-----------------视频上传之后的id为：-------------------" + videoId);
            return videoId;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }


    }

    /**
     * 删除多个视频
     *
     * @param videoIdList
     */
    @Override
    public void removeAlyVideo(List<String> videoIdList) {
        try {

            // 初始化对象
            DefaultAcsClient client = InitVdoClient.initVodClient(ConstantVodUtils.ACCESS_KEY_ID, ConstantVodUtils.ACCESS_KEY_SECRET);
            // 创建删除视频的Request
            DeleteVideoRequest request = new DeleteVideoRequest();
            // 设置视频id
            // 调用list集合中的将list集合中的数据转换成阿里云sdk所要求的
            String videoIds = StringUtils.join(videoIdList.toArray(), ",");
            request.setVideoIds(videoIds);
            // 调用初始化对象删除
            client.getAcsResponse(request);

        } catch (Exception e) {
            e.printStackTrace();
            throw new TjiseException(20001, "删除视频失败");

        }


    }
}


